package io.wmy.supex.framework.support.spring.condition;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * 数据源条件类
 * 判断是否初始化数据源或嵌入式数据库
 */
public class DataSourceCondition implements Condition {
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        String enableValue = conditionContext.getEnvironment().getProperty("enableEmbeddedDatabase");
        String initMethodValue = (String)annotatedTypeMetadata.getAnnotationAttributes(Bean.class.getName()).get("initMethod");
        if(initMethodValue.isEmpty()){
            return Boolean.valueOf(enableValue);
        }
        return !Boolean.valueOf(enableValue);
    }
}
